package com.xth8013;

import com.xth8013.bean.TicketingBean;
import com.xth8013.config.ConfigV2;
import com.xth8013.helper.GetJsonAndDeal;
import com.xth8013.helper.MusicUtil;
import com.xth8013.helper.QueryTickets;
import com.xth8013.helper.StringUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.ho.yaml.Yaml;

import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws Exception {

        List<ConfigV2> configList = Main.getConfigList();

        int i=0;
        while (true) {
            ++i;
            for (ConfigV2 config :configList) {
                try {
                    System.out.println(String.format("%d %s【%s->%s】 %s",i,config.getUserName(),config.getFromStation(),config.getToStation(),config.getDate()));
                    test(config);
                    Thread.sleep(10000);
                }catch (Exception e){
                    e.printStackTrace();
                    MusicUtil.play("E:\\KuGou\\ring08.wav");
                }
            }
            Thread.sleep(30000);
        }

    }


    public static void test(ConfigV2 config) throws Exception {
        List<TicketingBean> list = QueryTickets.queryTickets(config.getDate(), GetJsonAndDeal.getCodeByName(config.getFromStation()), GetJsonAndDeal.getCodeByName(config.getToStation()), config.getPurposeCodes());

        for (TicketingBean t : list) {
//            System.out.println(t.toStringLine());
            if (config.getStationTrainCodeList()!=null && config.getStationTrainCodeList().size()>0){
                if (!config.getStationTrainCodeList().contains(t.getStation_train_code())){
                    continue;
                }
            }

            boolean have = false;

            if (config.isNeedEd() && StringUtil.isNotBlank(t.getEd()) && !"无".equals(t.getEd()) ) {
                have = true;
            }
            if (config.isNeedYd() && StringUtil.isNotBlank(t.getYd()) && !"无".equals(t.getYd()) ) {
                have = true;
            }
            if (config.isNeedYz() && StringUtil.isNotBlank(t.getYz()) && !"无".equals(t.getYz()) ) {
                have = true;
            }

            if(have){
                System.out.println(t.toStringLine());
                MusicUtil.play("E:\\KuGou\\ring08.wav");
            }
        }
    }

    private static List<ConfigV2> getConfigList() throws FileNotFoundException, InvocationTargetException, IllegalAccessException {
        List<ConfigV2> list = new ArrayList<>();

        //如果读入Map,这里可以是Mapj接口,默认实现为LinkedHashMap
        List<Map<String,Object>> configMapList = Yaml.loadType(ConfigV2.class.getClassLoader().getResourceAsStream("application.yml"), ArrayList.class);

        ConfigV2 configV2 = null;
        for (Map<String, Object> m :configMapList) {
            configV2 = new ConfigV2();
            BeanUtils.populate(configV2,m);
            list.add(configV2);
        }
        System.out.println(list);
        return list;
    }
}
